#include "head.h"

void setcolor(const int x) {
#ifdef __WIN32__
  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
#endif
}
void print_color(int num) {
  setcolor(color[num]);
  printf("%s", name[num]);
  setcolor(7);
}
void output(struct User *tmp, int NO) {
  // printf("ID:%s score:%lf coin:%d padlock:%d key:%d time:%g steps:%d
  // life:%d\n",tmp->name,tmp->score,tmp->coin,tmp->padlock,tmp->key,tmp->time,tmp->tot_step,tmp->life);
  printf("===========================================\n");
  printf("NO.%d:\n", NO);
  printf("| ID:%s\n", tmp->name);
  printf("| score:%0f\n", tmp->score);
  printf("|More informations:\n");
  printf("| =>time:%g\n", tmp->time);
  printf("|   steps:%d\n", tmp->tot_step);
  printf("| =>life:%d\n", tmp->life);
  printf("|   coin:%d\n", tmp->coin);
  printf("|   key:%d\n", tmp->key);
  printf("|   padlock:%d\n", tmp->padlock);
  //	printf("===========================================\n");
}
void win() { //�һ�ںͿ�ʼһ�
  system("clear");
  system("color 3f");
  puts("\n");
  puts("                                                 ******                "
       "   ******     ****     ******      *****");
  puts("                                                    **        ***      "
       "    **         **       ****        **");
  puts("                                                    **       ** **     "
       "   **          **       ** **       **");
  puts("                                                     **      **  **    "
       "   **          **       **  **      **");
  puts("                                                     **     **    **   "
       "  **           **       **   **     **");
  puts("                                                      **    **    **   "
       "  **           **       **    **    **");
  puts("                                                      **   **      **  "
       " **            **       **     **   **");
  puts("                                                       **  **      **  "
       " **            **       **      **  **");
  puts("                                                       ** **        ** "
       "**             **       **       ** **");
  puts("                                                        ***          "
       "***             ****     *****       ****");
  puts("                                                                       "
       "                                                                       "
       "                                               \n");
  puts("please enter your name!\n");
  printf("ID :");
}
void display_card() {
  HideCursor();
  system("clear");
  static int namesize = 0;
  if (!namesize)
    for (int i = 0; i < 100 / (sizeof(char) * 10); ++i) // sizeof(name)==100
      namesize = max(namesize, strlen(name[i]));
  for (int j = MAPSIZE; j >= 0; --j) {
    for (int i = 0; i <= MAPSIZE; ++i) {
      if (visible[i][j]) {
        setcolor(7);
        int len = strlen(name[map[i][j]]);
        for (int k = 1; k <= namesize + 1 - len; ++k)
          printf(" ");
        if (i == nowx && j == nowy)
          setcolor(color[map[i][j]] + 16 * 7);
        else
          setcolor(color[map[i][j]]);
        printf("%s", name[map[i][j]]);
      } else {
        setcolor(7);
        printf("        %s", ".");
      }
    }
    setcolor(0);
    printf("0\n");
  }
  setcolor(7);
  printf("Your location (%d,%d) \n", nowx, nowy);
  printf("Now you are on the ");
  print_color(map[nowx][nowy]);
  printf("\ntotal steps %d\n", tot_steps);

  char bar[100] = " _�x�y�z�|�}�~�~�~�~";
  setcolor(4);
  printf("\nlife\t");
  for (int i = 1; i <= 2 * life; ++i)
    printf("%c", bar[i]);
  setcolor(color[6]);
  printf("\ncoin\t");
  for (int i = 1; i <= 2 * (coin + padlock); ++i)
    printf("%c", bar[i]);
  setcolor(color[6]);
  printf("\nkey\t");
  for (int i = 1; i <= 2 * key; ++i)
    printf("%c", bar[i]);
  setcolor(7);
  puts("\nplease inter the number");
  puts("8=up, 2=down, 6=right, 4=left,0=quit,5=cheat");
}

void judge(const int x, const int y) {
  make_it_vis(nowx + x, nowy + y);
  if (in_the_map(nowx + x, nowy + y)) {
    int next = map[nowx + x][nowy + y];
    if (next == 6 && key == 0) {
      flag = 0;
      nowx += x;
      nowy += y;
      display_card();
      printf("You cannot open the ");
      print_color(next);
      printf(" without ");
      print_color(4);
      printf("\n");
      needcls = 0;
    } else if (1 <= next && next <= 3) {
      ++tot_steps;
      display_card();
      printf("You cannot move on the ");
      print_color(next);
      printf("\n");
      needcls = 0;
    } else {
      ++tot_steps;
      if (next == 4)
        ++key;
      else if (next == 5)
        ++coin;
      else if (next == 6) {
        if (key > 0) {
          ++padlock;
          //					++coin;
          --key;
        }
      } else if (7 <= next && next <= 8)
        --life;
      if (flag)
        map[nowx][nowy] = 0;
      else
        flag = 1;
      nowx += x;
      nowy += y;
    }
  }
}